<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>自己的电商系统</title>
	<base href="http://localhost/shop/" />
	<link rel="stylesheet" type="text/css" href="__PUBLIC__/css/bootstrap.min.css">
	<link rel="stylesheet" type="text/css" href="__PUBLIC__/css/bootstrap-theme.min.css">
	<link rel="stylesheet" type="text/css" href="__PUBLIC__/css/style.css">
</head>
<body class="container">
	<div class="row">
		<div class="col-md-3">
			<a href="__ROOT__"><img id="logo" src="__PUBLIC__/img/logo.png" /></a>
		</div>
		<div class="col-md-9">
			<img style="height:50px;" class="pull-right" src="public/img/shop_step_1.jpg" />	
		</div>
	</div>
	<br />
	<form action="<?=U("/Home/shopping/createOrder")?>" method="post">
	<div class="row">
		<div class="col-md-12">
			<table class="table table-bordered">
				<tr>
					<th width="100"></th>
					<th>商品</th>
					<th width="150">价格</th>
					<th width="150">数量</th>
					<th width="120">小计</th>
					<th width="80">操作</th>
				</tr>
				<?php
					$sum = 0;
					foreach($carts as $c){
					$product =M('product')->where('id ='.$c['product_id'])->find();
				?>
				<tr class='t'>
					<td class="text-center"><input  class="is_select" value="<?=$c['id']?>"  type="checkbox"></td>
					<td><img style="width:60px;" src="__ROOT__<?=$product['img']?>"><a href="<?=U('/Home/product/detail/'.$product['id'])?>"><?=$product['title']?></a></td>
					<td>￥<?=$product['price']?></td>
					<td class="cart_num">
						<input name="product_id" type="hidden" class="product_id" value="<?=$c['product_id']?>"/>
						<span class="minus">-</span>
						<input name="num" type="text" class="n" value="<?=$c['num']?>"/>
						<span class="add">+</span>
					</td>
					<td>￥<?php echo $p = intval($product['price']) * intval($c['num'])?></td>

					<?php $sum = $sum +$p;?>
					<td><a href="<?=U('/Home/shopping/cartdel/id/'.$c['id'])?>">删除</a></td>
				</tr>
				<?php }?>
				<tr>
					<td  class="text-center">
						 <div class="checkbox">
						    <label>
						      <input type="checkbox" id='checkall'> 全选
						    </label>
						  </div>
					</td>
					<td colspan="10">
						<a id="delete_all" href="#">删除</a>
						<p class="text-right num">合计：￥<?=$sum?></p>
					</td>
				</tr>
			</table>
			<button id="checkout" type="submit" href="#" class="btn btn-danger pull-right">去结算</button>
		</div>
	</div>
	</form>
	<script type="text/javascript" src=" __PUBLIC__/js/jquery-3.1.0.min.js"></script>
	<script type="text/javascript">
		$(function(){
			$("#checkall").click(function(){    
	            $('.is_select').prop("checked",this.checked); 
			});
			$('#delete_all').click(function(){
				var ids = [];
				$('.is_select').each(function(i,item){
					if($(item).prop('checked')){
						ids.push($(item).val());
					}
				});
				if(ids.length == 0){
					alert('没有选中任何商品');
				}else{
					$.post("<?=U('/Home/shopping/cartdels')?>",{
						'ids':ids
					},function(data){
						console.log(data);
						if(data == 'ok'){
							location.reload()
						}
					})
				}
				return false;
			})
			$(".minus").click(function(){
				var id = $(this).parents('.t').find('.is_select').val();
				var num = parseInt($(this).parents('.cart_num').find('.n').val()) - 1;
				$.get('<?=U('/Home/shopping/updateCart')?>',{
					'id':id,
					'num':num
				},function(data){
					if(data == 'ok'){
						location.reload();
					}else{
						alert(data);
					}
				})
			})
			$(".add").click(function(){
				var id = $(this).parents('.t').find('.is_select').val();
				var num = parseInt($(this).parents('.cart_num').find('.n').val()) + 1;
				$.get('<?=U('/Home/shopping/updateCart')?>',{
					'id':id,
					'num':num
				},function(data){
					if(data == 'ok'){
						location.reload();
					}else{
						alert(data);
					}
				})
			})
			$('.n').blur(function(){
				var id = $(this).parents('.t').find('.is_select').val();
				var num = parseInt($(this).val());
				$.get('<?=U('/Home/shopping/updateCart')?>',{
					'id':id,
					'num':num
				},function(data){
					if(data == 'ok'){
						location.reload();
					}else{
						alert(data);
					}
				})
			})
			$('#checkout').click(function(){
				var data = [];
				$('.is_select').each(function(i,item){
					if ($(item).prop('checked')) {
						var element ={
							'product_id':$(this).parents('.t').find('.product_id').val(),
							'num':$(this).parents('.t').find('.n').val()
						}
						data.push(element);
					}
				})
				console.log(data);
				$.post('<?=U('/Home/shopping/preOrder')?>',{
					'data':data
				},function(data){
					if (data == 'ok') {
						window.location.href = "<?=U('/Home/shopping/createOrder')?>"
					}
				})
				return false;
			})
		})
	</script>
</body>
</html>